var e = require;
var t = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var n = (function () {
    function e() {
        this._resMap = new Map();
    }
    return (
        (e.getInstance = function () {
            return this._resLoader || (this._resLoader = new e()), this._resLoader;
        }),
        (e.destroy = function () {
            this._resLoader && (this._resLoader = null);
        }),
        (e.prototype._getResItem = function (e, t) {
            var o = cc.loader,
                n = o._cache[e];
            if (!n) {
                var a = o._getResUuid(e, t, !0);
                if (a) {
                    var i = o._getReferenceKey(a);
                    n = o._cache[i];
                }
            }
            return n;
        }),
        (e.prototype._makeLoadResArgs = function () {
            if (arguments.length < 1 || "string" != typeof arguments[0])
                return console.error("_makeLoadResArgs error " + arguments), null;
            for (var e = {url: arguments[0]}, t = 1; t < arguments.length; ++t)
                1 == t && cc.js.isChildClassOf(arguments[t], cc.RawAsset)
                    ? (e.type = arguments[t])
                    : t == arguments.length - 1 && "string" == typeof arguments[t]
                    ? (e.use = arguments[t])
                    : "function" == typeof arguments[t] &&
                      (arguments.length > t + 1 && "function" == typeof arguments[t + 1]
                          ? (e.onProgess = arguments[t])
                          : (e.onCompleted = arguments[t]));
            return e;
        }),
        (e.prototype._makeReleaseResArgs = function () {
            if (arguments.length < 1 || "string" != typeof arguments[0])
                return console.error("_makeReleaseResArgs error " + arguments), null;
            for (var e = {url: arguments[0]}, t = 1; t < arguments.length; ++t)
                "string" == typeof arguments[t] ? (e.use = arguments[t]) : (e.type = arguments[t]);
            return e;
        }),
        (e.makeUseKey = function (e, t, o) {
            return void 0 === t && (t = "none"), void 0 === o && (o = ""), "use_" + e + "_by_" + t + "_for_" + o;
        }),
        (e.prototype.getCacheInfo = function (e) {
            return this._resMap.has(e) || this._resMap.set(e, {refs: new Set(), uses: new Set()}), this._resMap.get(e);
        }),
        (e.prototype.loadRes = function () {
            var e = this,
                t = this._makeLoadResArgs.apply(this, arguments);
            console.time("loadRes|" + t.url);
            var o = function (o, n) {
                    var a = function (t, o) {
                            if (t && t.dependKeys && Array.isArray(t.dependKeys))
                                for (var n = 0, i = t.dependKeys; n < i.length; n++) {
                                    var r = i[n];
                                    e.getCacheInfo(r).refs.add(o);
                                    var s = cc.loader._cache[r];
                                    a(s, o);
                                }
                        },
                        i = e._getResItem(t.url, t.type);
                    if ((i && i.url && a(i, i.url), i)) {
                        var r = e.getCacheInfo(i.url);
                        r.refs.add(i.url), t.use && r.uses.add(t.use);
                    }
                    console.timeEnd("loadRes|" + t.url), t.onCompleted && t.onCompleted(o, n);
                },
                n =
                    (new Date().getTime(),
                    function (e, o, n) {
                        t.onProgess && t.onProgess(e, o, n);
                    }),
                a = cc.loader.getRes(t.url, t.type);
            a ? o(null, a) : cc.loader.loadRes(t.url, t.type, n, o);
        }),
        (e.prototype.releaseRes = function () {
            var e = this._makeReleaseResArgs.apply(this, arguments),
                t = this._getResItem(e.url, e.type);
            if (t) {
                var o = this.getCacheInfo(t.url);
                e.use && o.uses.delete(e.use), this._release(t, t.url);
            } else console.warn("releaseRes item is null " + e.url + " " + e.type);
        }),
        (e.prototype._release = function (e, t) {
            var o = this;
            if (e) {
                var n = this.getCacheInfo(e.url);
                n.refs.delete(t),
                    0 == n.uses.size &&
                        0 == n.refs.size &&
                        ((function (e, t) {
                            if (e && e.dependKeys && Array.isArray(e.dependKeys))
                                for (var n = 0, a = e.dependKeys; n < a.length; n++) {
                                    var i = a[n],
                                        r = cc.loader._cache[i];
                                    o._release(r, t);
                                }
                        })(e, t),
                        e.uuid
                            ? (cc.loader.release(e.uuid), cc.log("resloader release item by uuid :" + e.url))
                            : (cc.loader.release(e.url), cc.log("resloader release item by url:" + e.url)));
            }
        }),
        (e.prototype.checkReleaseUse = function () {
            var e = this._makeReleaseResArgs.apply(this, arguments),
                t = this._getResItem(e.url, e.type);
            if (!t) return console.log("cant release,item is null " + e.url + " " + e.type), !0;
            var o = this.getCacheInfo(t.url),
                n = !1,
                a = !1;
            return (
                (n = !(e.use && o.uses.size > 0 && (1 != o.uses.size || !o.uses.has(e.use)))),
                (a = !!((1 == o.refs.size && o.refs.has(t.url)) || 0 == o.refs.size)),
                n && a
            );
        }),
        (e._resLoader = null),
        e
    );
})();
o.default = n;
